package com.sos.core.game
{
	import com.sos.Shared;
	import com.sos.core.DataObject;
	import com.sos.logging.Dumper;
	import com.sos.logging.IDumpable;

	public class GameList extends DataObject implements IDumpable
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON Property names
		public static const 	JSON_PROPERTY_LIST:String		= "list";
		public static const 	JSON_PROPERTY_COUNT:String		= "gameCount";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _list:Vector.<GameListItem>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameList(data:Object = null)
		{
			// Init objects
			_list = new Vector.<GameListItem>();
			// Clear
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get length():uint 
		{
			return _list.length;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		private function indexOfById(id:uint):int
		{
			var index:int = Shared.INVALID_INDEX;
			for (var i:int = 0; i < _list.length; i++)
			{
				if (_list[i].id == id) break;
			}
			return index;
		}
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
			// Required fields
			if (!data.hasOwnProperty(JSON_PROPERTY_COUNT))	throwLoadMissingPropError(JSON_PROPERTY_COUNT);
		}
		
		override protected function fill(data:Object):void
		{
			// Value parsing and casting
			var raw_list_count:uint = uint(data[JSON_PROPERTY_COUNT]); 
			var list:Vector.<GameListItem> = new Vector.<GameListItem>();
			// Check if any game item is passed
			if (raw_list_count > 0)
			{
				// retrieve raw data
				var raw_list:Array = data[JSON_PROPERTY_LIST];
				// Objects parsing
				for (var i:int = 0; i < raw_list.length; i++)
				{
					list.push(new GameListItem(raw_list[i]));
				}
			}
			// Updating Game list
			_list.splice(0, _list.length);
			for (var j:int = 0; j < list.length; j++)
			{
				_list.push(list[j]);
			}			
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function addItem(item:GameListItem):void
		{
			_list.push(item);
		}
		
		public function getItem(index:uint):GameListItem
		{
			return _list[index];
		}
		
		public function copy(gameList:GameList):void
		{
			// Value parsing and casting
			var list:Vector.<GameListItem> = new Vector.<GameListItem>();
			// Objects parsing
			for (var i:int = 0; i < gameList.length; i++)
			{
				list.push(gameList[i].clone());
			}
			// Updating Game list
			_list.splice(0, _list.length);
			for (var j:int = 0; j < list.length; j++)
			{
				_list.push(list[j]);
			}	
		}
		
		public function clone():GameList
		{
			var gameList:GameList = new GameList();
			gameList.copy(this);
			return gameList;
		}
		
		override public function clear():void
		{
			_list.splice(0, _list.length);
		}
		
		public function dump(source:Array = null, indentDepth:int = 0):Array
		{
			return Dumper.dump(this, null, source, indentDepth);
		}
		
	}
}